package com.csyf.manager.controller.system;

import com.csyf.manager.common.CommonConst;
import com.csyf.manager.entity.SysMenu;
import com.csyf.manager.service.SysMenuService;
import com.csyf.manager.utils.ResultEntity;
import com.csyf.manager.utils.ResultUtli;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: casper
 * @Description: 菜单管理
 * @Date: Created in 2018/11/23 17:20
 */
@Controller
@RequestMapping("/menu")
public class SysMenuController {

    private static final String[] MENU_FLAG= {"1", "2"};

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 菜单列表
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public List<SysMenu> loadMenus(String flag) {
        List<SysMenu> menuList = new ArrayList<>();
        if (StringUtils.isBlank(flag) || MENU_FLAG[0].equals(flag)) {
            menuList = sysMenuService.selectAll(CommonConst.STATUS_VALID);
        } else if (MENU_FLAG[1].equals(flag)) {
            menuList = sysMenuService.selectAllMenuName(CommonConst.STATUS_VALID);
        }
        return menuList;
    }

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public ResultEntity addMenu(SysMenu menu) {
        try {
            int num = sysMenuService.insert(menu);
            if (num > 0) {
                return ResultUtli.DEFAULT_SUCCESS("添加菜单成功！", null);
            } else {
                return ResultUtli.DEFAULT_ERROR("添加菜单失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtli.DEFAULT_ERROR("添加菜单失败！");
        }
    }

    /**
     * 菜单详情
     * @param model
     * @param menuId
     * @return
     */
    @GetMapping("/edit")
    public String detail(Model model, String menuId) {
        SysMenu menu = sysMenuService.findByMenuId(menuId);
        if (null != menu) {
            if (menu.getParentId().equals(CommonConst.TOP_MENU_ID)) {
                model.addAttribute("parentName", CommonConst.TOP_MENU_NAME);
            } else {
                SysMenu parent = sysMenuService.findById(menu.getParentId());
                model.addAttribute("parentName", parent.getName());
            }
        }
        model.addAttribute("menu", menu);
        return "menu/detail";
    }

    /**
     * 编辑菜单
     * @param menu
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public ResultEntity edit(@ModelAttribute("menu") SysMenu menu) {
        int num = sysMenuService.updateByMenuId(menu);
        if (num > 0) {
            return ResultUtli.DEFAULT_SUCCESS("编辑菜单成功！", null);
        } else {
            return ResultUtli.DEFAULT_ERROR("编辑菜单失败！");
        }
    }


    @PostMapping("/delete")
    @ResponseBody
    public ResultEntity delete(String menuId) {
        try {
            //判断是否存在下级资源
            int subMenuCount = sysMenuService.selectSubMenuByMenuId(menuId);
            if (subMenuCount > 0) {
                return ResultUtli.DEFAULT_ERROR("该资源存在下级资源，无法删除！");
            }

            int num = sysMenuService.updateStatus(menuId, CommonConst.STATUS_INVALID);
            if (num > 0) {
                return ResultUtli.DEFAULT_SUCCESS("删除菜单成功！", null);
            } else {
                return ResultUtli.DEFAULT_ERROR("删除菜单失败！");
            }
        } catch (Exception e) {
            return ResultUtli.DEFAULT_ERROR("删除菜单失败！");
        }

    }



}
